package win.zxshuo.www.controller.apis;

import org.apache.http.HttpEntity;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import win.zxshuo.www.controller.responses.BaseResponse;
import win.zxshuo.www.model.ChapterModelMongoDB;
import win.zxshuo.www.model.chapters.Chapter2Model;
import win.zxshuo.www.model.chapters.ChapterModel;
import win.zxshuo.www.model.chapters.ChapterModelExample;
import win.zxshuo.www.repository.ChapterMongoRepository;
import win.zxshuo.www.schedule.SchedulerMethods;
import win.zxshuo.www.service.BookService;
import win.zxshuo.www.service.ChapterService;
import win.zxshuo.www.service.RedisService;
import win.zxshuo.www.spider.BaiduSearch;
import win.zxshuo.www.spider.BaseRequest;
import win.zxshuo.www.spider.JieqiSpider;
import win.zxshuo.www.spider.QidianSpider;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("api/test")
public class TestApiController {
    Log logger = LogFactory.getLog(getClass());

    @Autowired
    QidianSpider qidianSpider;
    @Autowired
    BaiduSearch baiduSearch;
    @Autowired
    BaseRequest baseRequest;
    @Autowired
    JieqiSpider jieqiSpider;
    @Autowired
    SchedulerMethods schedulerMethods;
    @Autowired
    BookService bookService;
    @Autowired
    ChapterService chapterService;
    @Autowired
    RedisService redisService;

    @Autowired
    ChapterMongoRepository chapterMongoRepository;



    @RequestMapping("/baidu")
    public BaseResponse getBySearchBaidu(String keyWord){
        BaseResponse baseResponse = new BaseResponse();
        baiduSearch.getSearchContent(keyWord);
        return baseResponse;
    }

    @RequestMapping("/qidian")
    public BaseResponse getQidianBookInfo(String url){
        BaseResponse baseResponse = new BaseResponse();
        try{
            qidianSpider.getBookinfo(url);
            baseResponse.setTrue();
        }catch (Exception e){
            baseResponse.setErrorMsg(e.getMessage());
        }

        return baseResponse;
    }
    @RequestMapping(value = "/qidianrank",method = {RequestMethod.POST,RequestMethod.GET})
    public BaseResponse mkQidianrank(String url){
        BaseResponse baseResponse = new BaseResponse();
        try {
            qidianSpider.getRankingBookinfo(url);
            baseResponse.setTrue();
        }catch (Exception e){
            baseResponse.setErrorMsg(e.getMessage());
        }
        return baseResponse;
    }





    @RequestMapping("/request")
    public BaseResponse baseRequestTest(String url){
        BaseResponse baseResponse = new BaseResponse();
        try{
           HttpEntity entity =  baseRequest.get(url);
           baseResponse.setTrue();
           baseResponse.setData(entity.getContentLength());
           List<String> locations = new ArrayList<>();
           baseRequest.getLocationsForUrlRedirect(url,locations);
           System.out.println(locations);
        }catch (Exception e){
            logger.error(e.getMessage());
        }


        return baseResponse;
    }

    @RequestMapping(value = "/jieqi",method = {RequestMethod.POST,RequestMethod.GET})
    public BaseResponse jieqiProtocol(String url){
        BaseResponse baseResponse = new BaseResponse();
        try {
//            jieqiSpider.getBookIndex(url);
//            jieqiSpider.startCrawler();
            schedulerMethods.spideBooksForUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }

        return baseResponse;
    }


    @RequestMapping("/nomarl")
    public BaseResponse momarl(){
        BaseResponse baseResponse = new BaseResponse();
        baseResponse.setData(bookService.getLatestChapterTableInUseAndUseNum());

        return baseResponse;
    }

    @RequestMapping("/addwordnum")
    public BaseResponse addwordNum(){
        BaseResponse baseResponse = new BaseResponse();
        bookService.addBookWordNum(1,2L);
        return baseResponse;
    }

    @RequestMapping("/relect")
    public BaseResponse testReflect(){
        BaseResponse baseResponse = new BaseResponse();
        Chapter2Model chapter2Model = new Chapter2Model();
        chapter2Model.setCreateDate(new Date());
        try {
            ChapterModel chapterModel = new ChapterModel(chapter2Model,Chapter2Model.class);
            System.out.println(chapterModel.getTitle());
        }catch (Exception e){
            logger.error(e.getCause());
        }


        return baseResponse;
    }

    @RequestMapping("/qrank")
    public BaseResponse getQidianRank(){
        BaseResponse baseResponse = new BaseResponse();
        try {
            qidianSpider.getRankingBookinfo("https://www.qidian.com/rank/yuepiao?style=1&page");
            qidianSpider.getRankingBookinfo("https://www.qidian.com/rank/hotsales?style=1&page");
            qidianSpider.getRankingBookinfo("https://www.qidian.com/rank/newvipclick?style=1&page");
            qidianSpider.getRankingBookinfo("https://www.qidian.com/rank/click?style=1&page");
            qidianSpider.getRankingBookinfo("https://www.qidian.com/rank/recom?style=1&page");
            qidianSpider.getRankingBookinfo("https://www.qidian.com/rank/collect?style=1&page");
            qidianSpider.getRankingBookinfo("https://www.qidian.com/rank/vipup?style=1&page");
            qidianSpider.getRankingBookinfo("https://www.qidian.com/rank/vipcollect?style=1&page");
            qidianSpider.getRankingBookinfo("https://www.qidian.com/rank/vipreward?style=1&page");
            qidianSpider.getRankingBookinfo("https://www.qidian.com/rank/signnewbook?style=1&page");
            qidianSpider.getRankingBookinfo("https://www.qidian.com/rank/pubnewbook?style=1&page");
            qidianSpider.getRankingBookinfo("https://www.qidian.com/rank/newsign?style=1&page");
            qidianSpider.getRankingBookinfo("https://www.qidian.com/rank/newauthor?style=1&page");
            logger.info("采集起点排行榜结束");
            //TODO 将起点女生的排行采集做好之后将会加入
        }catch (Exception e){
            logger.info("定时采集起点排行出错，请老板查看日志");
            logger.error(e.getCause());
        }

        return baseResponse;
    }

    @RequestMapping("/scheduler")
    public BaseResponse runScheduler(){
        BaseResponse baseResponse = new BaseResponse();
        schedulerMethods.spideBooksForUpdate();
        return baseResponse;
    }

    @RequestMapping("/testsave")
    public BaseResponse testRunSaving(){
        BaseResponse baseResponse = new BaseResponse();

        return baseResponse;
    }

    @GetMapping("/testredis")
    public BaseResponse testRedis(){
        BaseResponse baseResponse = new BaseResponse();
        redisService.saveObjForKey("good","不好");
        return baseResponse;
    }

    @GetMapping("/testmongo")
    public BaseResponse testMongo(){
        BaseResponse baseResponse = new BaseResponse();
//        ChapterModelExample chapterModelExample = new ChapterModelExample();
//        chapterModelExample.setId(100L);
//        chapterModelExample.setBookId(1l);
        ChapterModelMongoDB chapterModelMongoDB  = new ChapterModelMongoDB();
        chapterModelMongoDB.setBookId(1L);
        chapterMongoRepository.save(chapterModelMongoDB);
        return baseResponse;
    }

}
